import { reqAutoComplete, reqSearchTags } from "../../api/index"

Page({
  data:{
    keyword:'',
    keywordList:[],
    hotKeywordVOList:[],
    defaultKeyWord:'',
    schemeUrl:''
  },
  onShow(){
    this.getSearchTags()
  },
  async getSearchTags(){
    try {
      const res = await reqSearchTags()
      this.setData({
        hotKeywordVOList:res.data.hotKeywordVOList,
        defaultKeyWord:res.data.defaultKeyword?.keyword,
        schemeUrl:res.data.defaultKeyword.schemeUrl
      })
    } catch (error) {
      console.log(error)
      wx.showToast({
        title: '获取数据失败',
        icon:'error'
      })
    }
  },
  search(){
    if(!this.data.keyword){
      wx.navigateToMiniProgram({path:this.data.schemeUrl})
    }
    wx.navigateTo({
      url: `/pages/result/result?keyword=${this.data.keyword}`,
    })
  },
  async autoComplete(e){
    this.setData({keyword:await e.detail.value})
      try {
        const res = await reqAutoComplete(this.data.keyword)
        this.setData({
          keywordList:res.data
        })
      } catch (error) {
        console.log(error)
        wx.showToast({
          title: '获取参数失败',
          icon:'error'
        })
      }
  },
  clickTag(e){
    this.setData({keyword:e.target.dataset.keyword})
    this.search()
  },
  cancel(){
    this.setData({keyword:''})
    wx.navigateBack()
  }
})